<template>
  <div class="role-container">
        <div class="left-wrapper">
      <div v-for="(item,index) in roleList" :key="item.roleId" class="role-item" @click="menuChange(index)" :class="{active: index===activeIndex}">
        <div class="role-info">
          <svg-icon :icon-class="index===activeIndex?'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <div class="more">
            <el-dropdown>
              <span class="el-dropdown-link">
                <svg-icon icon-class="more" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="toEdit(item.roleId)">编辑角色</el-dropdown-item>
                <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </div> 
       <el-button class="addBtn" size="mini" @click="toAddRole">添加角色</el-button>
    </div>
    
    <div class="right-wrapper">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="基础权限" name="permission">
          <div class="tree-wrapper">
          <div v-for="item in treeList" :key="item.id" class="tree-item">
            <div class="tree-title"> {{ item.title }} </div>
            <el-tree
            ref="tree"
              show-checkbox
              :data="item.children"
              :props="defaultProps"
              node-key="id"
              />
          </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="成员管理(100)" name="employee">
          <el-table
    :data="roleUserList"
    stripe
    style="width: 100%">
    <el-table-column
      prop="id"
      label="id"
      width="180">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="180">
    </el-table-column>
    <el-table-column
      prop="userName"
      label="用户姓名">
    </el-table-column>
  </el-table>
        </el-tab-pane>
    </el-tabs>
  
</div>
  </div>
</template>

<script>
import { getProfileAPI, getTreeListApi,getRoleDetailApi, getRoleListApi,delRoleApi } from '@/api/system'
export default {
  name: 'Role',
  data(){
    return {
      roleList: [],
      activeIndex:0,
      treeList:[],
      defaultProps:{
        label:'title',
        disabled:function(){
          return true
        }
      },
      activeName:'permission',
      params:{
        page:1,
        pageSize:10
      },
      roleUserList:[],
      total:0
    }
  },
  async created() {
      await this.getProfileApiFn()
      // 确保this.getTreeList()返回的是一个promise才会等待
      await this.getTreeList()
      this.menuChange(0)
  },
  methods: {
    getProfileApiFn(){
      getProfileAPI().then((res)=>{
        this.roleList = res.data
      })
    },
    menuChange(index){
      this.activeIndex = index
      let roleId = this.roleList[this.activeIndex].roleId
      getRoleDetailApi(roleId).then((res)=>{
        console.log(res)
        const perms = res.data.perms
        const treeComponentList = this.$refs.tree
        treeComponentList.forEach((item,index)=>{
          item.setCheckedKeys(perms[index])
        })
      })
      this.getRoleUserList(roleId)
    },
    async getTreeList(){
      await getTreeListApi().then((res)=>{
        this.treeList = res.data
        // this.addDisabledProps(this.treeList)
      })
    },
    addDisabledProps(treeList){
      treeList.forEach((item)=>{
        item.disabled=true
        if(item.children){
          this.addDisabledProps(item.children)
        }
      })
      this.treeList = treeList
    },
    getRoleUserList(roleId){
      getRoleListApi(roleId,this.params).then(res=>{
        this.roleUserList = res.data.rows
        this.total = res.data.total
        console.log(res)
      })
    },
    toAddRole(){
      this.$router.push('/roleAdd')
    },
    toEdit(id){
      this.$router.push(`/roleAdd?id=${id}`)
    },
    delRole(roleId){
      this.$confirm('确定要删除该角色吗？','温馨提示').then((res)=>{
        delRoleApi(roleId).then((res)=>{
          this.$message.success('删除成功')
          this.getProfileApiFn()
        })
      })
      
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>